package org.jeecg.modules.quartz.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.util.JSONUtils;
import org.jeecg.board.service.LineBoardService;
import org.jeecg.client.BizResponse;
import org.jeecg.client.SendUtils;
import org.jeecg.common.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class LineBoardJob  implements Job {

    private String parameter;

    @Resource
    private LineBoardService lineBoardService;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HashMap<String, Object> map = new HashMap<>();
        Map<String,String> map2 = new HashMap<>();
        map2.put("serviceid","mes");
        map2.put("cmd","lineStatus");
        //map2.put("cmd","lineStatus");
        map2.put("entityid",this.parameter);
        map2.put("clientid",this.parameter+";;");
        map2.put("wo","0");
        map2.put("shift","0");
        map.put("line",this.parameter);
        BizResponse bizResponse = new BizResponse();
        try {
            bizResponse = SendUtils.sendRunCmd(map2);
            Map<String, String> data = bizResponse.getData();
            Map<String, String> shiftMap = JSON.parseObject(data.get("shift"), new TypeReference<Map<String, String>>(){});
            List<Map<String,Object>> listObjectFir = (List<Map<String,Object>>) JSONArray.parse(data.get("wo"));
            String woId = listObjectFir.get(0).get("id").toString();
            String currentPartNo = listObjectFir.get(0).get("pn").toString();
            BigDecimal qty = new BigDecimal(String.valueOf(listObjectFir.get(0).get("qty")));
            int startHour = (int)Double.parseDouble(shiftMap.get("StartHour"));
            int Hours = (int)Double.parseDouble(shiftMap.get("Hours"));
            String shiftName = shiftMap.get("Name");
            String shiftID = shiftMap.get("Id");
            LocalDateTime now = LocalDateTime.now();
            int hour = now.getHour();
            if (hour< startHour){
                now = now.plusDays(-1);
            }
            now = now.withHour(startHour);
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00");
            String start = now.format(pattern);
            LocalDateTime endDateTime = now.plusHours(Hours);
            String end = endDateTime.format(pattern);
            map.put("start",start);
            map.put("end",end);
            map.put("woid",woId);
            map.put("qty",qty.doubleValue());
            map.put("shiftName",shiftName);
            map.put("shiftID",shiftID);
            map.put("Hours",Hours);
            map.put("currentPartNo", currentPartNo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
        lineBoardService.getLineBoardProductionData(map);
        log.info( String.format("welcome %s! 看板定时任务3 SampleParamJob !   时间:" + DateUtils.now(), this.parameter));
    }
}
